﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.Exceptions.Database;

namespace BSCMFF.Web.UserControls
{
    public partial class SBUSelection : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //EntityDataSourceSBUs.ad
            //RadGrid rg = new RadGrid();
            //rg.DataSource = EntityDataSourceSBUs;
            //rg.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            //Page.Validate("TextRequired");
            if (Page.IsValid)
            {
                IRadTreeNodeContainer target = RadTreeView1;
                if (RadTreeView1.SelectedNode != null)
                {
                    RadTreeView1.SelectedNode.Expanded = true;
                    target = RadTreeView1.SelectedNode;
                }
                                

                BSCEntities context = new BSCEntities();
                SBU sbu = new SBU();
                sbu.Name = RadTextBoxNode.Text;
                if (RadTreeView1.SelectedNode != null)
                {
                    sbu.Superior_SBU_FK = Int32.Parse(RadTreeView1.SelectedNode.Value);
                }
                context.AddToSBUs(sbu);

                //addedNode.NodeTemplate
                
                
                try
                {
                    context.SaveChanges();

                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't create department: " + ex.Message);
                }

                RadTextBoxNode.Text = "";
                RadTreeNode addedNode = new RadTreeNode(sbu.Name, sbu.SBU_ID.ToString());
                //((Label)addedNode.Controls[1]).Text = RadTextBoxNode.Text;
                addedNode.Selected = true;
                
                target.Nodes.Add(addedNode);
                addedNode.DataBind();
                RadTreeView1_NodeClick(this, new RadTreeNodeEventArgs(addedNode));
            }
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            //name.Text = e.Node.Text;

        }

        protected void RadButtonDeleteNode_Click(object sender, EventArgs e)
        {
            RadTreeNode selectedNode = RadTreeView1.SelectedNode;

            

            Page.Validate("DeleteRequiresSelection");
            if (Page.IsValid)
            {
                BSCEntities context = new BSCEntities();
                DeleteDepartment(selectedNode, context);

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new Exception();
                }

                RadTreeView1.SelectedNode.Remove();
            }
            
        }

        private void DeleteDepartment(RadTreeNode selectedNode, BSCEntities context)
        {
            if (selectedNode.Nodes.Count > 0)
            {
                foreach (RadTreeNode n in selectedNode.Nodes)
                {
                    DeleteDepartment(n, context);
                }
            }

            

            int sbu_id = Int32.Parse(selectedNode.Value);

            SBU sbu = (from s in context.SBUs
                       where s.SBU_ID == sbu_id
                       select s).FirstOrDefault();

            if (sbu != null)
            {
                context.DeleteObject(sbu);
                //try
                //{
                //    context.SaveChanges();
                //}
                //catch
                //{
                //    throw new Exception();
                //}
                
            }


        }

        

    }
}